<?php
/**
 * $Horde: groo/item.php,v 1.42.2.5 2006/01/01 21:29:05 jan Exp $
 *
 * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @author  Luciano Ramalho <luciano@ramalho.org>
 * @since   Groo 1.0
 * @package Groo
 */

@define('GROO_BASE', dirname(__FILE__));
require_once GROO_BASE . '/lib/base.php';
require_once GROO_BASE . '/lib/ISBN.php';
require_once 'Horde/Prefs/CategoryManager.php';
$cManager = &new Prefs_CategoryManager();

/* Redirect to the collection view if no action has been requested. */
$item_id = Util::getFormData('item');
$itemlist_id = Util::getFormData('itemlist');
$actionID = Util::getFormData('actionID');
if (is_null($actionID)) {
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
}

/* Run through the action handlers. */
switch ($actionID) {
case 'add_item':
    /* Set up the item attributes. */
    $itemlist_id = Groo::getDefaultCollection();
    if (is_a($itemlist_id, 'PEAR_Error')) {
        $notification->push($itemlist_id, 'horde.error');
    }
    $item_id = null;
    $isbn = '';
    $item_category = '';

    $notification->push('document.isbn.isbn.focus();', 'javascript');
    break;

case 'save_item':
    /* Get the form values. */
    $item_id = Util::getFormData('item');
    $isbn = trim(Util::getFormData('isbn'));
    $item_category = Util::getFormData('item_category');
    $itemlist_original = Util::getFormData('itemlist_original');
    $collection_target = Util::getFormData('collection_target');

    if (ISBN::isValidISBN13($isbn)) {
        $isbn13 = $isbn;
    } elseif (ISBN::isValidISBN10($isbn)) {
        $isbn13 =ISBN::convertISBN10toISBN13($isbn);
    } else {
        $isbn13 = NULL;       
        $notification->push(sprintf(_('"%s" is not a valid ISBN-10, ISBN-13 or book EAN'), $isbn), 'horde.warning');
    }
    
    if (!is_null($isbn13)) {
        $share = $GLOBALS['groo_shares']->getShare($collection_target);
        if (is_a($share, 'PEAR_Error')) {
            $notification->push(sprintf(_("Access denied saving item: %s"), $share->getMessage()), 'horde.error');
        } elseif (!$share->hasPermission(Auth::getAuth(), PERMS_EDIT)) {
            $notification->push(sprintf(_("Access denied saving item to %s."), $share->get('name')), 'horde.error');
        } else {
            
            if ($new_category = Util::getFormData('new_category')) {
                $new_category = $cManager->add($new_category);
                if ($new_category) {
                    $item_category = $new_category;
                }
            }
    
            /* Creating a new item. */
            $storage = &Groo_Driver::singleton($collection_target);
            
            $book = $storage->getBookISBN13($isbn13);
            if (is_a($book, 'PEAR_Error')) {
                $notification->push($book, 'horde.error');
            } else {
                if($book) { 
                    $bookId = $book->recordId;
                    $title = $book->getTitle();
                    $message = _("The book \"%s\" was found in the catalog.");
                    $notification->push(sprintf($message, $title), 'horde.success');
                } else { /*add book to catalog*/
                    $result = $storage->addBookISBN13($isbn13,Auth::getAuth());
                    if (is_a($result, 'PEAR_Error')) {
                        $bookId = NULL;
                        $notification->push($result, 'horde.error');
                    } else {
                        $bookId = $result;
                        $title = 'ISBN-13: '.$isbn13;
                        $message = _("The book ISBN-13 \"%s\" was added to the catalog.");
                        $notification->push(sprintf($message, $isbn13), 'horde.success');
                    }
                }

                if (!is_null($bookId)) {
                    //     function addItem($desc, $bookId, $body='', $category = '', $uid = null)
                    $result = $item_id = $storage->addItem($title, $bookId, '', $item_category);
                    /* Check our results. */
                    if (is_a($result, 'PEAR_Error')) {
                        $notification->push(sprintf(_("There was an error saving the item: %s"), $result->getMessage()), 'horde.warning');
                    } else {
                        $isbn='';
                    }
                }
            }
        }
    } // if !is_null($isbn13)

    break;

default:
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
}

$title = _("Add a new book via ISBN or EAN barcode");

$notification->push('document.isbn.isbn.focus()', 'javascript');
$collections = Groo::listCollections(false, PERMS_EDIT);
require GROO_TEMPLATES . '/common-header.inc';
require GROO_TEMPLATES . '/menu.inc';
require GROO_TEMPLATES . '/isbn/isbn_form.inc';

echo '<p>&nbsp;</p>';

$title = _("Previously added items");


/* Get the full, sorted collection. */
$items = Groo::listItems($prefs->getValue('sortby'),
                          $prefs->getValue('sortdir'));
                          
Horde::addScriptFile('httpclient.js', 'horde', true);
Horde::addScriptFile('tooltip.js', 'horde', true);
Horde::addScriptFile('tables.js', 'groo', true);

// list display
require GROO_TEMPLATES . '/list/list.inc';

require $registry->get('templates', 'horde') . '/common-footer.inc';
